﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Crm.Views
{
    public enum PopupListeTipleri
    {
        Cari,
        Aday,
        Adres,
        Yetkili,
        CariGrup,
        CariSektor,
        CariBolge,
        CariTemsilci,
        CariVeAday,
        Stok
    }


    public partial class PopupList : ChildWindow
    {
        CrmClient.CrmServiceClient client = new CrmClient.CrmServiceClient();
        private Dictionary<string, object> _result = new Dictionary<string, object>();
        public Dictionary<string, object> Result { get { return _result; } }
        public PopupListeTipleri ListeTipi { get; set; }
        public string Filtre { get; set; }
        public bool KodunaGore { get; set; }

        public PopupList()
        {
            InitializeComponent();
            this.Loaded += PopupList_Loaded;
            client.FirmaListesiCompleted += client_FirmaListesiCompleted;
            grdListe.MouseDoubleClick += grdListe_MouseDoubleClick;
            client.AdresListeleCompleted += client_AdresListeleCompleted;
            client.YetkiliListeleCompleted += client_YetkiliListeleCompleted;

            client.CariGruplariListeleCompleted += client_CariGruplariListeleCompleted;
            client.CariBolgeListeleCompleted += client_CariBolgeListeleCompleted;
            client.CariTemsilciListeleCompleted += client_CariTemsilciListeleCompleted;
            client.CariSektorListeleCompleted += client_CariSektorListeleCompleted;
            client.StokListesiCompleted += client_StokListesiCompleted;
        }

        void client_StokListesiCompleted(object sender, CrmClient.StokListesiCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                grdListe.DataSource = e.Result;
                grdListe.Columns["ID"].Visible = false;
                grdListe.Columns["StokKod"].VisibleIndex = 0;
                grdListe.Columns["StokKod"].HeaderContent = "Stok Kodu";

                grdListe.Columns["Adi"].VisibleIndex = 1;
                grdListe.Columns["Adi"].HeaderContent = "Stok Adı";
                grdListe.Columns["Fiyat"].VisibleIndex = 2;
                grdListe.Columns["Fiyat"].HeaderContent = "Fiyatı";
            }
        }

        void client_CariSektorListeleCompleted(object sender, CrmClient.CariSektorListeleCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                grdListe.DataSource = e.Result;
                grdListe.Columns["ID"].Visible = false;
                grdListe.Columns["Adi"].VisibleIndex = 0;
                grdListe.Columns["Soyadi"].VisibleIndex = 1;
            }
        }

        void client_CariTemsilciListeleCompleted(object sender, CrmClient.CariTemsilciListeleCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                grdListe.DataSource = e.Result;
                grdListe.Columns["ID"].Visible = false;
                grdListe.Columns["Adi"].VisibleIndex = 0;
                grdListe.Columns["Soyadi"].VisibleIndex = 1;
            }
        }

        void client_CariBolgeListeleCompleted(object sender, CrmClient.CariBolgeListeleCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                grdListe.DataSource = e.Result;
                grdListe.Columns["ID"].Visible = false;
                grdListe.Columns["Adi"].VisibleIndex = 0;
                grdListe.Columns["Soyadi"].VisibleIndex = 1;
            }
        }

        void client_CariGruplariListeleCompleted(object sender, CrmClient.CariGruplariListeleCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                grdListe.DataSource = e.Result;
                grdListe.Columns["ID"].Visible = false;
                grdListe.Columns["Adi"].VisibleIndex = 0;
                grdListe.Columns["Soyadi"].VisibleIndex = 1;
            }
        }

        void client_YetkiliListeleCompleted(object sender, CrmClient.YetkiliListeleCompletedEventArgs e)
        {
            grdListe.DataSource = e.Result;
            grdListe.Columns["ID"].Visible = false;
            grdListe.Columns["Adi"].VisibleIndex = 0;
            grdListe.Columns["Soyadi"].VisibleIndex = 1;
        }

        void client_AdresListeleCompleted(object sender, CrmClient.AdresListeleCompletedEventArgs e)
        {
            grdListe.DataSource = e.Result;
            grdListe.Columns["ID"].Visible = false;
            grdListe.Columns["No"].VisibleIndex = 0;
            grdListe.Columns["Adresi"].VisibleIndex = 1;
        }

        void grdListe_MouseDoubleClick(object sender, EventArgs e)
        {
            Secim();
        }

        void client_FirmaListesiCompleted(object sender, CrmClient.FirmaListesiCompletedEventArgs e)
        {
            grdListe.DataSource = e.Result;
            grdListe.Columns["Adi"].HeaderContent = "Firma Ünvanı";
            grdListe.Columns["ID"].Visible = false;
            grdListe.Columns["Kodu"].HeaderContent = "Firma Kodu";
            grdListe.Columns["Kodu"].Width = 100;
            grdListe.Columns["Kodu"].VisibleIndex = 0;
            grdListe.Columns["Adi"].VisibleIndex = 1;
            grdListe.Columns["Adi"].Width = 250;
            grdListe.Columns["HesapTipi"].Width = 50;
            if (ListeTipi != PopupListeTipleri.CariVeAday)
                grdListe.Columns["HesapTipi"].Visible = false;
        }

        void PopupList_Loaded(object sender, RoutedEventArgs e)
        {
            switch (ListeTipi)
            {
                case PopupListeTipleri.Cari:
                    client.FirmaListesiAsync(Filtre.Trim(), KodunaGore, 0);
                    break;
                case PopupListeTipleri.Aday:
                    client.FirmaListesiAsync(Filtre.Trim(), KodunaGore, 1);
                    break;
                case PopupListeTipleri.Adres:
                    client.AdresListeleAsync(Filtre.Trim());
                    break;
                case PopupListeTipleri.Yetkili:
                    client.YetkiliListeleAsync(Filtre.Trim().Split(';')[0], int.Parse(Filtre.Trim().Split(';')[1]));
                    break;
                case PopupListeTipleri.CariBolge:
                    client.CariBolgeListeleAsync();
                    break;
                case PopupListeTipleri.CariGrup:
                    client.CariGruplariListeleAsync();
                    break;
                case PopupListeTipleri.CariSektor:
                    client.CariSektorListeleAsync();
                    break;
                case PopupListeTipleri.CariTemsilci:
                    client.CariTemsilciListeleAsync();
                    break;
                case PopupListeTipleri.CariVeAday:
                    client.FirmaListesiAsync(Filtre.Trim(), KodunaGore, 2);
                    break;
                case PopupListeTipleri.Stok:
                    client.StokListesiAsync(Filtre.Trim());
                    break;
                default:
                    break;
            }
            grdListe.Focus();
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            Secim();
        }

        private void Secim()
        {
            for (int i = 0; i < grdListe.Columns.Count; i++)
            {
                _result.Add(grdListe.Columns[i].FieldName, grdListe.GetCellValue(grdListe.FocusedRowHandle, grdListe.Columns[i]));
            }
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}

